<template>

  <div class="prodetail">
    <!-- 头部 -->
    <van-nav-bar fixed title="商品详情页" left-arrow @click-left="$router.go(-1)" />

    <!-- 内容页 -->
    <div class="content" v-if="isShow">
      <van-swipe :autoplay="3000" @change="onChange">
        <van-swipe-item v-for="(image, index) in images" :key="index">
          <img :src="image" />
        </van-swipe-item>

        <template #indicator>
          <div class="custom-indicator">{{ current + 1 }} / {{ images.length }}</div>
        </template>
      </van-swipe>

      <!-- 商品说明 -->
      <div class="info">
        <div class="title">
          <div class="price">
            <span class="now">￥{{ goodsPrice }}</span>
            <span class="oldprice">￥6699.00</span>
          </div>
          <div class="sellcount">已售1001件</div>
        </div>
        <div class="msg text-ellipsis-2">
         {{ goodsName }}
        </div>

        <div class="service">
          <div class="left-words">
            <span><van-icon name="passed" />七天无理由退货</span>
            <span><van-icon name="passed" />48小时发货</span>
          </div>
          <div class="right-icon">
            <van-icon name="arrow" />
          </div>
        </div>
      </div>


      <!-- 商品评价 -->
      <div class="comment">
        <div class="comment-title">
          <div class="left">商品评价 (5条)</div>
          <div class="right">查看更多 <van-icon name="arrow" /> </div>
        </div>
        <div class="comment-list">
          <div class="comment-item" v-for="item in 3" :key="item">
            <div class="top">
              <img src="@/assets/img/user.jpg" alt="">
              <div class="name">神雕大侠</div>
              <van-rate :size="16" :value="5" color="#ffd21e" void-icon="star" void-color="#eee" />
            </div>
            <div class="content">
              质量很不错 挺喜欢的
            </div>
            <div class="time">
              2023-03-21 15:01:35
            </div>
          </div>
        </div>
      </div>


      <!-- 商品描述 -->
      <div class="desc">
        <img src="https://uimgproxy.suning.cn/uimg1/sop/commodity/kHgx21fZMWwqirkMhawkAw.jpg" alt="">
        <img src="https://uimgproxy.suning.cn/uimg1/sop/commodity/0rRMmncfF0kGjuK5cvLolg.jpg" alt="">
        <img src="https://uimgproxy.suning.cn/uimg1/sop/commodity/2P04A4Jn0HKxbKYSHc17kw.jpg" alt="">
        <img src="https://uimgproxy.suning.cn/uimg1/sop/commodity/MT4k-mPd0veQXWPPO5yTIw.jpg" alt="">
      </div>



      <!-- 底部 -->
      <div class="footer">
        <div class="icon-home" @click="$router.push('/')">
          <van-icon name="wap-home-o" />
          <span>首页</span>
        </div>
        <div class="icon-cart" @click="$router.push('/cart')">
          <van-icon name="shopping-cart-o" />
          <span>购物车</span>
        </div>
        <div class="btn-add" @click="addCart()">加入购物车</div>
        <div class="btn-buy" @click="buyOrder">立刻购买</div>
      </div>

      

    </div>

    <van-empty v-else description="商品未上架" />
  </div>

</template>

<script>
import { getGoodsDetail } from '@/api/goodsDetail/getDetails'
import { Toast } from 'vant'
import { addCar } from '@/api/car/index'


export default {
  name: 'ProDetail',
  data() {
    return {
      images: [
      ],
      current: 0,
      goodsId: '',
      goodsName: '',
      goodsPrice: '',
      goodsImg: '',
      isShow: false,
    }
  },
  created() {
    this.getDetail()
  },
  methods: {
    onChange(index) {
      this.current = index
    },

    async getDetail() {
      const id = this.$route.query.id
      console.log(id);
      

      const res = await getGoodsDetail(id)
      console.log(res);

      // 没有token
      if (res.status === 408){
				Toast.fail(res.message);
				this.$router.push({path: '/login'})
			}

      if (res.status === 200) {
        console.log(res.data);
        const {g_id, g_name, g_price, imgUrl} = res.data
        
        this.isShow = true

        this.goodsId = g_id
        this.goodsName = g_name
        this.goodsPrice = g_price
        this.images.push(imgUrl)
      } else {
        this.isShow = false
      }
    },

    async addCart(){
      const g_id = this.$route.query.id
      console.log(g_id);

      const res = await addCar(g_id)
      console.log(res);
      
      if (res.status === 200){
        Toast.success('加入购物车成功')
      }else if(res.status === 201){
        Toast.fail(res.msg);
      } else{
        Toast.fail('加入购物车失败')
      }
    },


    buyOrder() {
      this.$router.push({
        name: 'buyorder',
        params:{
          g_id: this.goodsId,
          g_name: this.goodsName,
          g_price: this.goodsPrice,
          imgUrl: this.images[0]
        }
      })
    }



  }
}
</script>

<style lang="less" scoped>
.prodetail {
  padding-top: 46px;

  ::v-deep .van-icon-arrow-left {
    color: #333;
  }

  img {
    display: block;
    width: 100%;
  }

  .custom-indicator {
    position: absolute;
    right: 10px;
    bottom: 10px;
    padding: 5px 10px;
    font-size: 12px;
    background: rgba(0, 0, 0, 0.1);
    border-radius: 15px;
  }

  .desc {
    width: 100%;
    overflow: scroll;

    ::v-deep img {
      display: block;
      width: 100% !important;
    }
  }

  .info {
    padding: 10px;
  }

  .title {
    display: flex;
    justify-content: space-between;

    .now {
      color: #fa2209;
      font-size: 20px;
    }

    .oldprice {
      color: #959595;
      font-size: 16px;
      text-decoration: line-through;
      margin-left: 5px;
    }

    .sellcount {
      color: #959595;
      font-size: 16px;
      position: relative;
      top: 4px;
    }
  }

  .msg {
    font-size: 16px;
    line-height: 24px;
    margin-top: 5px;
  }

  .service {
    display: flex;
    justify-content: space-between;
    line-height: 40px;
    margin-top: 10px;
    font-size: 16px;
    background-color: #fafafa;

    .left-words {
      span {
        margin-right: 10px;
      }

      .van-icon {
        margin-right: 4px;
        color: #fa2209;
      }
    }
  }

  .comment {
    padding: 10px;
    margin-top: 30px;
    margin-bottom: 30px;
  }

  .comment-title {
    display: flex;
    justify-content: space-between;

    .left {
      font-size: 25px;
    }

    .right {
      color: #959595;
      font-size: 17px;
    }
  }

  .comment-item {
    font-size: 16px;
    line-height: 30px;

    .top {
      height: 30px;
      display: flex;
      align-items: center;
      margin-top: 20px;

      img {
        width: 20px;
        height: 20px;
      }

      .name {
        margin: 0 10px;
      }
    }

    .time {
      color: #999;
    }
  }

  .footer {
    position: fixed;
    left: 0;
    bottom: 0;
    width: 100%;
    height: 55px;
    background-color: #fff;
    border-top: 1px solid #ccc;
    display: flex;
    justify-content: space-evenly;
    align-items: center;

    .icon-home,
    .icon-cart {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      font-size: 14px;

      .van-icon {
        font-size: 24px;
      }
    }

    .btn-add,
    .btn-buy {
      height: 36px;
      line-height: 36px;
      width: 120px;
      border-radius: 18px;
      background-color: #ffa900;
      text-align: center;
      color: #fff;
      font-size: 14px;
    }

    .btn-buy {
      background-color: #fe5630;
    }
  }
}

.tips {
  padding: 10px;
}
</style>